﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;
using LocalSettings;
using QuickTasks.ViewModels.Data;
using Microsoft.Advertising.Mobile.UI;

namespace QuickTasks.Views.Message
{
    public partial class SendEmail : PhoneApplicationPage
    {
        RetriveData retriveemail = new RetriveData();
        QuickMessages objQuickMessages = new QuickMessages();
        List<QuickMessageProp> objQuickMessageList = new List<QuickMessageProp>();
        string Qid = string.Empty;
      
        public SendEmail()
        {
            InitializeComponent();
          
            Loaded += new RoutedEventHandler(SendEmail_Loaded);
        }
     
        void SendEmail_Loaded(object sender, RoutedEventArgs e)
        {
            string Qtype = "";
            try
            {
                if (App.EmailSubject != "") tbxSubject.Text = App.EmailSubject;
                if (App.EmailMessage != "") tbxEmailmessage.Text = App.EmailMessage;




                if (NavigationContext.QueryString.TryGetValue("quickId", out Qid))
                {

                    if (NavigationContext.QueryString.TryGetValue("quicktype", out Qtype))
                    {
                        List<QuickMessageProp> objmessage = new List<QuickMessageProp>();
                        using (QuickTaskDataContext context = new QuickTaskDataContext(App.ConnectionString))
                        {
                            var favpeoplelist = from i in context.message where i.id.ToString() == Qid select i;
                            foreach (var favpeople in favpeoplelist)
                            {
                                tbxSubject.Text = favpeople.subject;
                                App.TrEmailId = favpeople.reciepient;
                                App.TcEmailId = favpeople.cc;
                                App.EmailId = retriveemail.remails(App.TrEmailId);
                                App.EmailId1 = retriveemail.cemails(App.TcEmailId);
                                tbxEmailmessage.Text = favpeople.message;
                                break;
                            }
                        }
                    }
                }
                IEnumerable<System.Windows.Navigation.JournalEntry> back = NavigationService.BackStack;
                foreach (var b in back)
                {
                    if (b.Source.ToString().Contains("SendEmail.xaml"))
                        NavigationService.RemoveBackEntry();
                    break;
                }
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in  SendEmail_Loaded event In SendEmail.xaml page", ex);
            }
           
        }

        private void ThemeableImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            App.EmailSubject = tbxSubject.Text;
            App.EmailMessage = tbxEmailmessage.Text;
         
            string type = "Rec";
            NavigationService.Navigate(new Uri("/Views/FavoritePeople/FavoriteEmails.xaml?type="+type+"&nav=1", UriKind.Relative));
        }
        private void ThemeableImage_MouseLeftButtonDown1(object sender, MouseButtonEventArgs e)
        {
            App.EmailSubject = tbxSubject.Text;
            App.EmailMessage = tbxEmailmessage.Text;
          
            string type = "cc";
            NavigationService.Navigate(new Uri("/Views/FavoritePeople/FavoriteEmails.xaml?type=" + type + "&nav=1", UriKind.Relative));
        }
      
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (App.EmailId == "")
                {
                    tblkRecipientEmailValidation.Visibility = Visibility.Visible;
                    return;
                }
                else
                    tblkRecipientEmailValidation.Visibility = Visibility.Collapsed;
                if (tbxSubject.Text == "")
                {
                    tblkSubjectValidation.Visibility = Visibility.Visible;
                    return;
                }
                else
                    tblkSubjectValidation.Visibility = Visibility.Collapsed;

                if (tbxEmailmessage.Text == "")
                {
                    tblkMessageValidation.Visibility = Visibility.Visible;
                    return;
                }
                else
                    tblkMessageValidation.Visibility = Visibility.Collapsed;


                objQuickMessages.SendMessage(App.EmailId1, App.EmailId, tbxSubject.Text, tbxEmailmessage.Text, "Email");
                App.EmailMessage = "";
                App.EmailSubject = "";
                NavigationService.Navigate(new Uri("/MainPage.xaml?sectionId=1", UriKind.Relative));
            }
            catch (Exception ex)
            {
                Exceptions.SaveOrSendExceptions("Exception in  ApplicationBarIconButton_Click event In SendEmail.xaml page", ex);
            }
        }
    }

}
